package com.jinzhi.jzapi.utils;


import com.jinzhi.jzweb.utils.ValuesUtil;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

/**
 * @author mengzhichao
 * @create 2023-11-27-10:26
 */
public class CertificatesUtil {

    public static String certificatesImge(String name, String type, String number, String webAddress, String stuImg, String QRImg, String backgroundPath, String year, String math) throws IOException {

        System.out.println("学生寸照+++++++++++++++++++"+stuImg);
        System.out.println("二维码+++++++++++++++++++"+QRImg);
        System.out.println("证书模板路径+++++++++++++++++++"+backgroundPath);

        //将实际尺寸转换成英寸，再按照96像素/英寸比例计算得到大小
        BufferedImage backgroundImage = resizeImage(1123, 794, ImageIO.read(new File(backgroundPath)));


        //在背景图片中添加入需要写入的信息，
        Graphics2D g = backgroundImage.createGraphics();
        //统一对文字去除锯齿边框
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //设置为透明覆盖
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));

        Color color = new Color(0, 0, 0);
        g.setColor(color);
        g.setFont(new Font("黑体", Font.BOLD, 32));
        //考生姓名
        g.drawString(name, name.length()>3?150:175, 352);


        //寸照图片
        BufferedImage stuImgs = resizeImage(100, 145, ImageIO.read(new File(stuImg)));
        g.drawImage(stuImgs, 790, 140, stuImgs.getWidth(), stuImgs.getHeight(), null);
        //二维码图片
        BufferedImage qrCodeImage = resizeImage(100, 100, ImageIO.read(new File(QRImg)));
        g.drawImage(qrCodeImage, 915, 168, qrCodeImage.getWidth(), qrCodeImage.getHeight(), null);

        g.setFont(new Font("黑体", Font.BOLD, 23));
        //证书类型
        g.drawString(type, 688, 350);
        //证书编号
        g.drawString(number, 488, 491);
        //查询网址
        g.drawString(webAddress, 488, 536);
        //年
        g.drawString(year, 318, 350);
        //月
        g.drawString(math, 415, 350);

        //完成模板修改
        g.dispose();
        String outPutPath = ValuesUtil.localUrl + "/upload/traSynthesis/" + name + number + ".png";
        // 输出新的图片
        ImageIO.write(backgroundImage, "png", new File(outPutPath));
        System.out.println("证书生成完成路径++++++++++++++++++++"+outPutPath);

        return outPutPath;
    }


//    static void main(String[] args) throws IOException {
//        certificatesImge1("张三丰","保育师师资证书","PXCHSTC230700005","www.baidu.com","D:/upload/stuInchPhoto1/pic20656624565.PNG","D:/upload/ewm.jpg","D:/upload/gwnlpxzs.png","2023","07");
//    }


    /**
     * 重新设置图片大小
     *
     * @param width
     * @param height
     * @param bufferedImage
     * @return
     *      
     */
    private static BufferedImage resizeImage(int width, int height, BufferedImage bufferedImage) {
        BufferedImage newBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        newBufferedImage.getGraphics().drawImage(bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
        return newBufferedImage;
    }

    public static String certificatesImge1(String name, String type, String number, String webAddress, String stuImg, String QRImg, String backgroundPath, String year, String math) throws IOException {

        System.out.println("学生寸照+++++++++++++++++++"+stuImg);
        System.out.println("二维码+++++++++++++++++++"+QRImg);
        System.out.println("证书模板路径+++++++++++++++++++"+backgroundPath);

        //将实际尺寸转换成英寸，再按照96像素/英寸比例计算得到大小
        BufferedImage backgroundImage = resizeImage(1123, 794, ImageIO.read(new File(backgroundPath)));


        //在背景图片中添加入需要写入的信息，
        Graphics2D g = backgroundImage.createGraphics();
        //统一对文字去除锯齿边框
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //设置为透明覆盖
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));

        Color color = new Color(0, 0, 0);
        g.setColor(color);
        g.setFont(new Font("黑体", Font.BOLD, 32));
        //考生姓名
        g.drawString(name, name.length()>3?150:175, 317);


        //寸照图片
        BufferedImage stuImgs = resizeImage(100, 145, ImageIO.read(new File(stuImg)));
        g.drawImage(stuImgs, 830, 140, stuImgs.getWidth(), stuImgs.getHeight(), null);
        //二维码图片
        BufferedImage qrCodeImage = resizeImage(100, 100, ImageIO.read(new File(QRImg)));
        g.drawImage(qrCodeImage, 950, 168, qrCodeImage.getWidth(), qrCodeImage.getHeight(), null);

        g.setFont(new Font("黑体", Font.BOLD, 23));
        //证书类型
//        g.drawString(type, 688, 350);
        //证书编号
        g.drawString(number, 488, 512);
        //查询网址
        g.drawString(webAddress, 488, 555);
        g.setFont(new Font("黑体", Font.BOLD, 26));
        //年
        g.drawString(year, 323, 314);
        //月
        g.drawString(math, 415, 314);

        //完成模板修改
        g.dispose();
        String outPutPath = ValuesUtil.localUrl + "/upload/traSynthesis/" + name + number + ".png";
        // 输出新的图片
        ImageIO.write(backgroundImage, "png", new File(outPutPath));
        System.out.println("证书生成完成路径++++++++++++++++++++"+outPutPath);

        return outPutPath;
    }

//    public static void main(String[] args) throws IOException {
//        certificatesImge5("张三��","保育师师资证书","PXCHSTC230700005","www.baidu.com","D:/upload/stuInchPhoto1/pic20656624565.PNG","D:/upload/ewm.jpg","D:/upload/gwnlpxzs.png","2023","07");
//    }
private static void setupGraphics(Graphics2D g) {
    // 统一对文字去除锯齿边框
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    // 设置为透明覆盖
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));
}

    private static void drawText(Graphics2D g, String text, int x, int y) {
        g.drawString(text, x, y);
    }

    private static BufferedImage readAndResizeImage(String imagePath, int width, int height) throws IOException {
        File file = new File(imagePath);
        if (!file.exists()) {
            throw new IOException("图像文件不存在: " + file.getAbsolutePath());
        }
        BufferedImage originalImage = ImageIO.read(file);
        if (originalImage == null) {
            throw new IOException("无法读取图像文件: " + file.getAbsolutePath());
        }
        return resizeImage(width, height, originalImage);
    }

    public static String certificatesImge5(String name, String type, String number, String webAddress, String stuImg, String qrImg, String backgroundPath, String year, String math) throws IOException {
        System.out.println("学生寸照+++++++++++++++++++" + stuImg);
        System.out.println("二维码+++++++++++++++++++" + qrImg);
        System.out.println("证书模板路径+++++++++++++++++++" + backgroundPath);

        // 检查并读取背景图片
        File backgroundFile = new File(backgroundPath);
        if (!backgroundFile.exists()) {
            throw new IOException("背景图片文件不存在: " + backgroundFile.getAbsolutePath());
        }
        BufferedImage backgroundImage = resizeImage(1123, 794, ImageIO.read(backgroundFile));

        // 创建 Graphics2D 对象
        Graphics2D g = backgroundImage.createGraphics();
        try {
            setupGraphics(g); // 设置 Graphics2D 的属性

            // 设置颜色和字体
            Color color = new Color(0, 0, 0);
            g.setColor(color);
            g.setFont(new Font("黑体", Font.BOLD, 32));

            // 考生姓名
            drawText(g, name, name.length() > 3 ? 122 : 147, 351);

            // 处理寸照图片
            BufferedImage stuImgs = readAndResizeImage(stuImg, 100, 145);
            g.drawImage(stuImgs, 830, 140, stuImgs.getWidth(), stuImgs.getHeight(), null);

            // 处理二维码图片
            BufferedImage qrCodeImage = readAndResizeImage(qrImg, 100, 100);
            g.drawImage(qrCodeImage, 950, 168, qrCodeImage.getWidth(), qrCodeImage.getHeight(), null);

            // 设置字体大小
            g.setFont(new Font("黑体", Font.BOLD, 23));
            // 证书编号
            drawText(g, number, 488, 510);
            // 查询网址
            drawText(g, webAddress, 488, 555);

            // 设置字体大小
            g.setFont(new Font("黑体", Font.BOLD, 27));
            // 年
            drawText(g, year, 288, 350);
            // 月
            drawText(g, math, 385, 350);

        } finally {
            g.dispose(); // 确保 Graphics2D 被释放
        }

        // 输出新的图片
        String outPutPath = ValuesUtil.localUrl + "/upload/traSynthesis/" + name + number + ".png";
        File outputFile = new File(outPutPath);
        ImageIO.write(backgroundImage, "png", outputFile);
        System.out.println("证书生成完成路径++++++++++++++++++++" + outPutPath);

        return outPutPath;

    }

    public static String certificatesImge4(String name, String type, String number, String webAddress, String stuImg, String qrImg, String backgroundPath, String year, String math) throws IOException {
        System.out.println("学生寸照+++++++++++++++++++"+stuImg);
        System.out.println("二维码+++++++++++++++++++"+qrImg);
        System.out.println("证书模板路径+++++++++++++++++++"+backgroundPath);

        //将实际尺寸转换成英寸，再按照96像素/英寸比例计算得到大小
        BufferedImage backgroundImage = resizeImage(1123, 794, ImageIO.read(new File(backgroundPath)));


        //在背景图片中添加入需要写入的信息，
        Graphics2D g = backgroundImage.createGraphics();
        //统一对文字去除锯齿边框
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //设置为透明覆盖
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));

        Color color = new Color(0, 0, 0);
        g.setColor(color);
        g.setFont(new Font("黑体", Font.BOLD, 32));
        //考生姓名
        g.drawString(name, name.length()>3?75:95, 351);


        //寸照图片
        BufferedImage stuImgs = resizeImage(100, 145, ImageIO.read(new File(stuImg)));
        g.drawImage(stuImgs, 830, 140, stuImgs.getWidth(), stuImgs.getHeight(), null);
        //二维码图片
        BufferedImage qrCodeImage = resizeImage(100, 100, ImageIO.read(new File(qrImg)));
        g.drawImage(qrCodeImage, 950, 168, qrCodeImage.getWidth(), qrCodeImage.getHeight(), null);

        g.setFont(new Font("黑体", Font.BOLD, 23));
        //证书类型
//        g.drawString(type, 688, 350);
        //证书编号
        g.drawString(number, 488, 511);
        //查询网址
        g.drawString(webAddress, 488, 555);
        g.setFont(new Font("黑体", Font.BOLD, 27));

        // 年
        drawText(g, year, 238, 349);
        // 月
        drawText(g, math, 335, 349);

        //完成模板修改
        g.dispose();
        String outPutPath = ValuesUtil.localUrl + "/upload/traSynthesis/" + name + number + ".png";
        // 输出新的图片
        ImageIO.write(backgroundImage, "png", new File(outPutPath));
        System.out.println("证书生成完成路径++++++++++++++++++++"+outPutPath);

        return outPutPath;
    }

    public static String certificatesImge3(String name, String type, String number, String webAddress, String stuImg, String qrImg, String backgroundPath, String year, String math) throws IOException {
        System.out.println("学生寸照+++++++++++++++++++"+stuImg);
        System.out.println("二维码+++++++++++++++++++"+qrImg);
        System.out.println("证书模板路径+++++++++++++++++++"+backgroundPath);

        //将实际尺寸转换成英寸，再按照96像素/英寸比例计算得到大小
        BufferedImage backgroundImage = resizeImage(1123, 794, ImageIO.read(new File(backgroundPath)));


        //在背景图片中添加入需要写入的信息，
        Graphics2D g = backgroundImage.createGraphics();
        //统一对文字去除锯齿边框
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //设置为透明覆盖
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));

        Color color = new Color(0, 0, 0);
        g.setColor(color);
        g.setFont(new Font("黑体", Font.BOLD, 32));
        //考生姓名
        g.drawString(name, name.length()>3?75:95, 350);


        //寸照图片
        BufferedImage stuImgs = resizeImage(100, 145, ImageIO.read(new File(stuImg)));
        g.drawImage(stuImgs, 830, 140, stuImgs.getWidth(), stuImgs.getHeight(), null);
        //二维码图片
        BufferedImage qrCodeImage = resizeImage(100, 100, ImageIO.read(new File(qrImg)));
        g.drawImage(qrCodeImage, 950, 168, qrCodeImage.getWidth(), qrCodeImage.getHeight(), null);

        g.setFont(new Font("黑体", Font.BOLD, 23));
        //证书类型
//        g.drawString(type, 688, 350);
        //证书编号
        g.drawString(number, 488, 510);
        //查询网址
        g.drawString(webAddress, 488, 555);
        g.setFont(new Font("黑体", Font.BOLD, 27));
        // 年
        drawText(g, year, 237, 349);
        // 月
        drawText(g, math, 330, 349);

        //完成模板修改
        g.dispose();
        String outPutPath = ValuesUtil.localUrl + "/upload/traSynthesis/" + name + number + ".png";
        // 输出新的图片
        ImageIO.write(backgroundImage, "png", new File(outPutPath));
        System.out.println("证书生成完成路径++++++++++++++++++++"+outPutPath);

        return outPutPath;
    }

    public static String certificatesImge2(String name, String type, String number, String webAddress, String stuImg, String qrImg, String backgroundPath, String year, String math) throws IOException {
        System.out.println("学生寸照+++++++++++++++++++"+stuImg);
        System.out.println("二维码+++++++++++++++++++"+qrImg);
        System.out.println("证书模板路径+++++++++++++++++++"+backgroundPath);

        //将实际尺寸转换成英寸，再按照96像素/英寸比例计算得到大小
        BufferedImage backgroundImage = resizeImage(1123, 794, ImageIO.read(new File(backgroundPath)));


        //在背景图片中添加入需要写入的信息，
        Graphics2D g = backgroundImage.createGraphics();
        //统一对文字去除锯齿边框
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //设置为透明覆盖
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));

        Color color = new Color(0, 0, 0);
        g.setColor(color);
        g.setFont(new Font("黑体", Font.BOLD, 32));
        //考生姓名
        g.drawString(name, name.length()>3?115:135, 350);


        //寸照图片
        BufferedImage stuImgs = resizeImage(100, 145, ImageIO.read(new File(stuImg)));
        g.drawImage(stuImgs, 830, 140, stuImgs.getWidth(), stuImgs.getHeight(), null);
        //二维码图片
        BufferedImage qrCodeImage = resizeImage(100, 100, ImageIO.read(new File(qrImg)));
        g.drawImage(qrCodeImage, 950, 168, qrCodeImage.getWidth(), qrCodeImage.getHeight(), null);

        g.setFont(new Font("黑体", Font.BOLD, 23));
        //证书类型
//        g.drawString(type, 688, 350);
        //证书编号
        g.drawString(number, 488, 510);
        //查询网址
        g.drawString(webAddress, 488, 555);
        g.setFont(new Font("黑体", Font.BOLD, 27));
        // 年
        drawText(g, year, 283, 349);
        // 月
        drawText(g, math, 385, 349);


        //完成模板修改
        g.dispose();
        String outPutPath = ValuesUtil.localUrl + "/upload/traSynthesis/" + name + number + ".png";
        // 输出新的图片
        ImageIO.write(backgroundImage, "png", new File(outPutPath));
        System.out.println("证书生成完成路径++++++++++++++++++++"+outPutPath);

        return outPutPath;
    }

//    static String certificatesImge(String name, String type, String number, String webAddress, String stuImg, String QRImg, String backgroundPath, String year, String math) throws IOException {
//
//        //将实际尺寸转换成英寸，再按照96像素/英寸比例计算得到大小计算得到大小
//        BufferedImage backgroundImage = resizeImage(1123, 794, ImageIO.read(new File(backgroundPath)));
//
//
//        //在背景图片中添加入需要写入的信息，
//        Graphics2D g = backgroundImage.createGraphics();
//        //统一对文字去除锯齿边框
//        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
//        //设置为透明覆盖
//        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));
//
//        Color color = new Color(0, 0, 0);
//        g.setColor(color);
//        g.setFont(new Font("黑体", Font.BOLD, 32));
//        //考生姓名
//        g.drawString(name, 173, 352);
//
//
//        //寸照图片
//        BufferedImage stuImgs = resizeImage(100, 145, ImageIO.read(new File(stuImg)));
//        g.drawImage(stuImgs, 790, 140, stuImgs.getWidth(), stuImgs.getHeight(), null);
//        //二维码图片
//        BufferedImage qrCodeImage = resizeImage(100, 100, ImageIO.read(new File(QRImg)));
//        g.drawImage(qrCodeImage, 915, 168, qrCodeImage.getWidth(), qrCodeImage.getHeight(), null);
//
//        g.setFont(new Font("黑体", Font.BOLD, 23));
//        //证书类型
//        g.drawString(type, 683, 350);
//        //证书编号
//        g.drawString(number, 488, 491);
//        //查询网址
//        g.drawString(webAddress, 488, 536);
//        //年
//        g.drawString(year, 323, 350);
//        //月
//        g.drawString(math, 418, 350);
//
//        //完成模板修改
//        g.dispose();
//        String outPutPath = ValuesUtil.localUrl + "/upload/traSynthesis/" + name + number + ".png";
//        // 输出新的图片
//        ImageIO.write(backgroundImage, "png", new File(outPutPath));
//
//        return outPutPath;
//    }
//
//
//
//
//
//    public static String jinzhiPdf(String traSynthesis) throws IOException {
//        //将实际尺寸转换成英寸，再按照96像素/英寸比例计算得到大小
////        BufferedImage backgroundImage1 = ImageIO.read(new File(backgroundPath));//如果想获得原始图片，可直接用imageIo
//        BufferedImage backgroundImage = resizeImage(750, 1300, ImageIO.read(new File(traSynthesis)));
//
//        //在背景图片中添加入需要写入的信息，
//        Graphics2D g = backgroundImage.createGraphics();
//
//        //统一对文字去除锯齿边框
//        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
//        //设置为透明覆盖
//        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));
//
//        Color color = new Color(0, 0, 0);
//        g.setColor(color);
//        g.setFont(new Font("仿宋", Font.PLAIN, 16));
//
//        //姓名
//        g.drawString("曾xx", 175, 262);
//        //性别
//        g.drawString("男", 321, 262);
//        //出生日期
//        g.drawString("2024-04-24", 430, 262);
//
//        //学历
//        g.drawString("大学", 172, 300);
//        //所学专业
//        g.drawString("软件工作", 342, 300);
//
//        //证件类型
//        g.drawString("身份证", 172, 340);
//        //证件号码
//        g.drawString("430681199206150179", 346, 340);
//
//        //手机号码
//        g.drawString("15013011311", 167, 380);
//        //人员类别
//        if ("学生") {
//            g.setFont(new Font("仿宋", Font.PLAIN, 14));
//            g.drawString("√", 348, 374);
//        }
//        if ("企业员工") {
//            g.setFont(new Font("仿宋", Font.PLAIN, 14));
//            g.drawString("√", 391, 374);
//        }
//        if ("社会人员") {
//            g.setFont(new Font("仿宋", Font.PLAIN, 14));
//            g.drawString("√", 465, 374);
//        }
//        g.setFont(new Font("仿宋", Font.PLAIN, 16));
//        //工作单位
//        g.drawString("上海深圳湖南长沙金职伟业", 172, 414);
//
//        /** 循环获取职业等级证书情况，集合不能大于四*/
//        int y = 4;
//        int z = 0;
//        int u=1;
//        for (int x = 0; x < y; x++) {
//            //序号
//            g.drawString(String.valueOf(u), 155, 480 + z);
//            //工种
//            g.drawString("育婴师", 178, 480 + z);
//            //等级
//            g.drawString("初级", 311, 480 + z);
//            //证书号
//            g.drawString("12350670516548", 375, 480 + z);
//            //发证日期
//            g.drawString("2024-05-06", 550, 480 + z);
//            z = z + 25;
//            u++
//        }
//
//        //申报职业
//        g.drawString("测试", 300, 586);
//        //申报等级
//        g.drawString("四级", 480, 586);
//
//        //申报科目
//        if ("理论") {
//            g.setFont(new Font("仿宋", Font.PLAIN, 15));
//            g.drawString("√", 184, 616);
//        }
//        if ("实操") {
//            g.setFont(new Font("仿宋", Font.PLAIN, 15));
//            g.drawString("√", 244, 616);
//        }
//        if ("综合") {
//            g.setFont(new Font("仿宋", Font.PLAIN, 12));
//            g.drawString("√", 303, 618);
//        }
//        g.setFont(new Font("仿宋", Font.PLAIN, 16));
//        z=0;
//        for (int x = 0; x < y; x++) {
//            //起止年月
//            g.setFont(new Font("仿宋", Font.PLAIN, 12));
//            g.drawString("2024/3/2-2024/5/6", 85, 708+ z );
//            g.setFont(new Font("仿宋", Font.PLAIN, 15));
//            //工作单位
//            g.drawString("深圳公司上海公司长沙", 191, 708+ z );
//            g.setFont(new Font("仿宋", Font.PLAIN, 13));
//            //工作岗位
//            g.drawString("程序员不错", 396, 708 + z);
//            g.setFont(new Font("仿宋", Font.PLAIN, 16));
//            //单位联系人
//            g.drawString("曾中中中", 470, 708 + z);
//            //联系电话
//            g.drawString("15607000000", 548, 708+ z );
//            z = z + 30;
//        }
//
////        if(informationImportUserVo.getPersonPhoto()!=null){
//            //寸照，以水印的形式插入
//            //水印图象的路径 水印一般为gif或者png的，这样可设置透明度
//        ImageIcon imgIcon = new ImageIcon("D:/upload/liangcun.jpg");
//        Image img = imgIcon.getImage();
//
//        // 计算寸照的缩放比例
//        double scaleWidth = 114.0 / img.getWidth(null);
//        double scaleHeight = 183.0 / img.getHeight(null);
//
//        // 取缩放比例较小的值，保证不失真
//        double scale = Math.min(scaleWidth, scaleHeight);
//
//        // 计算新的宽度和高度
//        int newWidth = (int) (img.getWidth(null) * scale);
//        int newHeight = (int) (img.getHeight(null) * scale);
//
//        // 裁剪图片以保持比例
//        BufferedImage scaledImg = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
//        Graphics2D g2 = scaledImg.createGraphics();
//        g2.drawImage(img, 0, 0, newWidth, newHeight, null);
//        g2.dispose();
//
//        // 计算图片放置位置
//        int x = 550;
//        int  f= 260;
//
//        // 绘制图片
//        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
//        g.drawImage(scaledImg, x, f, null);
////        }
//
//
//        //完成模板修改
//        g.dispose();
//
//        String outPutPath;
//
//
//        outPutPath = ValuesUtil.localUrl + "/upload/traSynthesisNoSale/" + "kaoshenshenbaobiao1" + ".jpg";
//
//
//        // 输出新的图片
//        ImageIO.write(backgroundImage, "jpg", new File(outPutPath));
//        return outPutPath;
//
//
//    }
//
//




//
//    public static void main(String[] args) {
//        try {
//            certificatesImge6("张三丰","PXCHSTC230700005","D:/gwnl_zs.jpg","2025","07");
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//    }





    public static String certificatesImge6(String name,String number, String backgroundPath, String year, String math) throws IOException {


        // 检查并读取背景图片
        File backgroundFile = new File(backgroundPath);
        if (!backgroundFile.exists()) {
            throw new IOException("背景图片文件不存在: " + backgroundFile.getAbsolutePath());
        }
        BufferedImage backgroundImage = resizeImage(1123, 794, ImageIO.read(backgroundFile));

        // 创建 Graphics2D 对象
        Graphics2D g = backgroundImage.createGraphics();
        try {
            setupGraphics(g); // 设置 Graphics2D 的属性

            // 设置颜色和字体
            Color color = new Color(0, 0, 0);
            g.setColor(color);
            g.setFont(new Font("黑体", Font.BOLD, 32));

            // 考生姓名
            drawText(g, name, name.length() > 3 ? 169 : 205, 351);


            // 设置字体大小
            g.setFont(new Font("黑体", Font.BOLD, 23));
            // 证书编号
            drawText(g, number, 457, 480);


            // 设置字体大小
            g.setFont(new Font("黑体", Font.BOLD, 25));
            // 落证日期 - 年
            drawText(g, year, 348, 350);
            // 落证日期 - 月
            drawText(g, math, 442, 350);


            Date date=new Date();
            // 使用Calendar来获取年月日
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            String n = String.valueOf(calendar.get(Calendar.YEAR));  // 年（不需要补零）
            String y = String.format("%02d", calendar.get(Calendar.MONTH) + 1);  // 月（补零）
            String r = String.format("%02d", calendar.get(Calendar.DAY_OF_MONTH));  // 日（补零）
            // 落款日期
            g.setFont(new Font("黑体", Font.BOLD, 20));
            // 落证日期 - 年
            drawText(g, n, 227, 602);
            // 落证日期 - 月
            drawText(g, y, 302, 602);
            // 落证日期 - 日
            drawText(g, r, 355, 602);

        } finally {
            g.dispose(); // 确保 Graphics2D 被释放
        }

        // 输出新的图片
        String outPutPath = ValuesUtil.localUrl + "/upload/traSynthesis/" + name + number + ".png";
        File outputFile = new File(outPutPath);
        ImageIO.write(backgroundImage, "png", outputFile);
        System.out.println("证书生成完成路径++++++++++++++++++++" + outPutPath);

        return outPutPath;

    }


}
